<?php

/**
 * This is the model class for table "website".
 *
 * The followings are the available columns in table 'website':
 * @property integer $identification
 * @property string $shop_name
 * @property string $logo
 * @property string $logo_footer
 * @property string $banner
 * @property string $slogan
 * @property string $company_name
 * @property string $copyright
 * @property string $description
 * @property string $tax_id
 * @property string $business_regiCert
 * @property string $note
 * @property string $insert
 * @property string $update
 */
class Website extends CActiveRecord
{
    public $key;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Website the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'website';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('shop_name, insert', 'required'),
            array('shop_name, logo, logo_footer, banner, slogan, company_name, copyright, business_regiCert', 'length', 'max'=>255),
            array('tax_id', 'length', 'max'=>100),
            array('description, note, update', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('identification, shop_name, logo, logo_footer, banner, slogan, company_name, copyright, description, tax_id, business_regiCert, note, insert, update', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'identification' => 'Identification',
            'shop_name' => 'Shop Name',
            'logo' => 'Logo',
            'logo_footer' => 'Logo Footer',
            'banner' => 'Banner',
            'slogan' => 'Slogan',
            'company_name' => 'Company Name',
            'copyright' => 'Copyright',
            'description' => 'Description',
            'tax_id' => 'Tax',
            'business_regiCert' => 'Business Regi Cert',
            'note' => 'Note',
            'insert' => 'Insert',
            'update' => 'Update',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('identification',$this->identification);
        $criteria->compare('shop_name',$this->shop_name,true);
        $criteria->compare('logo',$this->logo,true);
        $criteria->compare('logo_footer',$this->logo_footer,true);
        $criteria->compare('banner',$this->banner,true);
        $criteria->compare('slogan',$this->slogan,true);
        $criteria->compare('company_name',$this->company_name,true);
        $criteria->compare('copyright',$this->copyright,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('tax_id',$this->tax_id,true);
        $criteria->compare('business_regiCert',$this->business_regiCert,true);
        $criteria->compare('note',$this->note,true);
        $criteria->compare('insert',$this->insert,true);
        $criteria->compare('update',$this->update,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


    /** getDataByID =============================
     * @author   TuanNguyen
     * @desc     get website record by ID
     * @param    integer $i_id
     * @return   Website Entity
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function getDataByID($i_id)
    {
        //Set SQL command
        $command = Yii::app()->db->createCommand("CALL website_02_getid(:p_id)");

        //Set parameter for SQL command
        $command->bindValue("p_id", $i_id,PDO::PARAM_INT);

        try
        {
            $row = $command->queryRow();

            if($row !=null)
            {
                $m_model = new Website();
                $m_model->unsetAttributes();

                $m_model->identification    = $row["identification"];
                $m_model->key               = $row["identification"];
                $m_model->shop_name         = $row["shop_name"];
                $m_model->logo              = $row["logo"];
                $m_model->logo_footer       = $row["logo_footer"];
                $m_model->banner            = $row["banner"];
                $m_model->slogan            = $row["slogan"];
                $m_model->company_name      = $row["company_name"];
                $m_model->copyright         = $row["copyright"];
                $m_model->description       = $row["description"];
                $m_model->tax_id            = $row["tax_id"];
                $m_model->business_regiCert = $row["business_regiCert"];
                $m_model->note              = $row["note"];
                $m_model->insert            = $row["insert"];
                $m_model->update            = $row["update"];

                return $m_model;
            }

            return null;
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_1";
            $o_error->param     = null;
            return $o_error;
        }
    }//End getDataByID


    /** addDataToDb =============================
     * @author   TuanNguyen
     * @desc     save product, product image entity to database
     * @param    string $p_insertStaement: string for values in insert statement
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function addDataToDb(Website $o_website)
    {
        $command = Yii::app()->db->createCommand("CALL website_03_insert
            (
                :p_shopName,
                :p_logo,
                :p_logoFooter,
                :p_banner,
                :p_slogan,
                :p_companyName,
                :p_copyright,
                :p_description,
                :p_taxID,
                :p_businessRegiCert
            )");

        $command->bindValue("p_shopName", $o_website->shop_name );
        $command->bindValue("p_logo", $o_website->logo);
        $command->bindValue("p_logoFooter",$o_website->logo_footer);
        $command->bindValue("p_banner",$o_website->banner);
        $command->bindValue("p_slogan",$o_website->slogan);
        $command->bindValue("p_companyName",$o_website->company_name);
        $command->bindValue("p_copyright",$o_website->copyright);
        $command->bindValue("p_description",$o_website->description);
        $command->bindValue("p_taxID",$o_website->tax_id);
        $command->bindValue("p_businessRegiCert",$o_website->business_regiCert);

        try
        {
            $i_result = $command->execute();

            if($i_result >=1)
                return true;

            return false;
        }//End try
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_1";
            $o_error->param     = null;
            return $o_error;
        }//End catch
    }//End addDataToDb


    /** updateDataToDb =============================
     * @author   TuanNguyen
     * @desc     update website record
     * @param    Wevsite Entity $o_website
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function updateDataToDb(Website $o_website)
    {
        $command = Yii::app()->db->createCommand("CALL website_04_update
            (
                :p_key,
                :p_shopName,
                :p_logo,
                :p_logoFooter,
                :p_banner,
                :p_slogan,
                :p_companyName,
                :p_copyright,
                :p_description,
                :p_taxID,
                :p_businessRegiCert
            )");

        $command->bindValue("p_key", $o_website->key);
        $command->bindValue("p_shopName", $o_website->shop_name);
        $command->bindValue("p_logo", $o_website->logo);
        $command->bindValue("p_logoFooter",$o_website->logo_footer);
        $command->bindValue("p_banner",$o_website->banner);
        $command->bindValue("p_slogan",$o_website->slogan);
        $command->bindValue("p_companyName",$o_website->company_name);
        $command->bindValue("p_copyright",$o_website->copyright);
        $command->bindValue("p_description",$o_website->description);
        $command->bindValue("p_taxID",$o_website->tax_id);
        $command->bindValue("p_businessRegiCert",$o_website->business_regiCert);

        try
        {
            $i_result = $command->execute();

            if($i_result >=1)
                return true;

            return false;
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_1";
            $o_error->param     = null;
            return $o_error;
        }
    }//End updateDataToDb
}